package com.study.spring.ch03.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.study.spring.ch03.pojo.User;

@Controller
@SessionAttributes(value = { "user" }) // ①
@RequestMapping(value = "/ch03/sessionParamBind/**") // ①处理器的通用映射前缀
public class SessionParamsBindController {

//	//6、@SessionAttributes绑定命令对象到session；
//	//1、在控制器类头上添加@SessionAttributes注解

	/**
	 * 2、@ModelAttribute注解的方法进行表单引用对象的创建
	 * 
	 * @return
	 */
	@ModelAttribute("user") // ②
	public User initUser() {
		User user = new User();
		user.setUsername("init-username");
		return user;

	}

	/**
	 * http://127.0.0.1:8080/spring/ch03/sessionParamBind/sessionAdd?username=newuser
	 * 3、@RequestMapping注解方法的@ModelAttribute注解的参数进行命令对象的绑定
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping("/sessionAdd") // ③
	public String session1(@ModelAttribute("user") User user,
			@Value("#{systemProperties['java.vm.version']}") String jvmVersion) {
		return null;
	}

	/**
	 * 4、通过SessionStatus的setComplete()方法清除@SessionAttributes指定的会话数据
	 * 
	 * @param user
	 * @param status
	 * @return
	 */
	@RequestMapping("/session2") // ③
	public String session(@ModelAttribute("user") User user, SessionStatus status) {
		if (true) { // ④
			status.setComplete();
		}
		return "success";
	}

}
